/**
 * User interface class to the CustomerContainer
 */
package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.CustomerCTR;
import ModelLayer.*;


public class CustomerContainerUI
{
   private CustomerCTR cctr;
    
   /**
     * Constructor for objects of class CustomerContainerUI
     */
    public CustomerContainerUI()
    {
      cctr = new CustomerCTR();
    }
    /**
     * Menu Section
     * 
     * start(), executes the menu.
     * customerMenu(), is a selection method for the user menu.
     * CustomerContainerMenu(), is a visual interpretation of the user menu for the customer section. 
     */
    public void start()
    {
        customerMenu();
    }
    public void customerMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeCustomerContainerMenu();
            if(choise == 1)
            {
                startGetCustomer();
            }
            else
            {
                if(choise == 2)
                {
                    startAddCustomer();
                }
                else
                {
                    if(choise == 3)
                    {
                    startDeleteCustomer();
                    }
                    else
                    {
                        if(choise == 4)
                        {
                            startEditPerson();
                        }
                        else
                        {
                            exit = true;
                        }
                    }
                }
               
                
            }
        }
    }
     public int writeCustomerContainerMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Customer Menu ***");
        System.out.println(" (1) Show information about a customer");
        System.out.println(" (2) Add customer");
        System.out.println(" (3) Delete customer");
        System.out.println(" (4) Update customer");
        System.out.println(" (5) Return");
        System.out.print("\n Make your choice: ");
        

        int choise = keyboard.nextInt();
        return choise;
    }
    /**
     * User input section
     * inputFirstName(), inputs the persons first name via user interaction.
     * inputLastName(), inputs the persons last name via user interaction.
     * inputZipCode(), inputs the persons postal code via user interaction.
     * inputCity(), inputs the persons city via user interaction.
     * inputPhone(), inputs the persons phone via user interaction.
     * inputAddress(), inputs the persons address via user interaction.
     * 
     */ 
    public String inputFirstName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write firstname:  ");
         String firstName = keyboard.nextLine();
         return firstName;
    }
     public String inputLastName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write lastname:  ");
         String lastName = keyboard.nextLine();
         return lastName;
    }
     public String inputAddress()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write address:  ");
         String address = keyboard.nextLine();
         return address;
    }
     public String inputZipCode()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write zipcode:  ");
         String zipcode = keyboard.nextLine();
         return zipcode;
    }
     public String inputCity()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write city:  ");
         String city = keyboard.nextLine();
         return city;
    }
     public String inputPhone()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.print(" Write phoneNo:  ");
         String phoneNo = keyboard.nextLine();
         return phoneNo;
    }
    /**
     * User interaction section
     * 
     * startAddCustomer(), creates a customer in CustomerContainer and stores it in array.
     * startGetCustomer(), finds the customer information trough there phonenumber via its perspectiv array.
     * startDeleteCustomer(), deletes customer if there phonenumber is found in array.
     * startEditCustomer(), edits the customers information after there phonenumber is found via there perspectiv arrays.
     * editPersonData(), is a visual interpretation of the customers edit menu.
     * startListAllCustomers(), lists all customers in array.
     */
    private void startAddCustomer()
    {
        String firstName = inputFirstName();
        String lastName = inputLastName();
        String address = inputAddress();
        String zipcode = inputZipCode();
        String city = inputCity();
        String phoneNo = inputPhone();
        cctr.createCustomer(firstName, lastName, address, zipcode, city, phoneNo);
    }
    
    private int editPersonData() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Edit first name");
        System.out.println(" (2) Edit last name");
        System.out.println(" (3) Edit address");
        System.out.println(" (4) Edit zipcode");
        System.out.println(" (5) Edit city");
        System.out.println(" (6) Edit phone number");
        int choise = keyboard.nextInt();
        return choise;
    }
    private void startDeleteCustomer() 
    {
        String phone = inputPhone();
        
        try
        {
            if(cctr.deleteCustomer(phone))
            {
                System.out.println("\n **Person deleted! **");
                Thread.sleep(1500);
            }
            else
            {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
    private void startGetCustomer()
    {
        String phone = inputPhone();
        
        Customer customer = null;
        customer = cctr.getCustomer(phone);
        
        try 
        {
            if (customer != null)
            {
                 System.out.println(" -- Person Information --");
                 System.out.println("Name: " + customer.getLastName() + ", " + customer.getFirstName());
                 System.out.println("Address: " + customer.getAddress());
                 System.out.println("Zip code: " + customer.getZipcode());
                 System.out.println("City: " + customer.getCity());
                 System.out.println("Phone number: " + customer.getPhoneNo());
                 System.out.println("");
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
            }
            else
            {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
        
    }
    private void startEditPerson() 
    {
        String phone = inputPhone();
        
        Customer customer = null;
        customer = cctr.getCustomer(phone);
        
        try
        {
            if(customer != null)
            {
                int nr = editPersonData();
                Scanner keyboardData = new Scanner(System.in); 
                System.out.print(" Enter new data: ");
                String newData = keyboardData.nextLine();
                cctr.updateCustomerData(customer, nr, newData);
            }
            else
            {
                System.out.println("\n ** No person with that phone number was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
}
